<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2019/1/15
 * Time: 15:22
 */
namespace app\shop\controller;
use think\Db;
class Task extends Base
{
    /**
     * 任务管理数据
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function homePage(){
        extract(input());
        $search = input('param.search/a');
        $page = input('param.page');
        if(!$page){
            $pages = 0;
        }else{
            $pages = $page-1;
        }

        $search = $this->search($search, $page);
        //搜索条件
        $map = [];
        //任务名称
        if($search[0] != ""){
            $map['task_name'] = ['like',"%" . $search[0] . "%"];
        }
        //任务类型
        if($search[1] != ""){
            $map['task_type'] = $search[1];
        }
        $map['task_status'] = ['<>',5];//必须放到状态筛选的上面来
//        //上架下架
        if($search[2] != ""){
            if($search[2] == 1){
                $map['task_status'] = array('in','1,2,3');
            }else{
                $map['task_status'] = 4;
            }

        }
        //普通、活动
        if($search[3] != ""){
            $map['task_active'] = $search[3];
        }
        $field = ['task_id','task_number','task_s_id','task_num',"IF(task_num!=0,CONCAT(left((task_done_num/task_num)*100,5),'%'),'0%')"=>'complete','task_name',"(CASE WHEN task_type = 1 THEN '下载' WHEN task_type = 2 THEN '求赞求粉' WHEN task_type = 3 THEN '问卷' WHEN task_type = 4 THEN '投票' WHEN task_type = 5 THEN '视频' ELSE '分享' END)"=>'task_type','task_sheng','task_price','task_money','task_status','task_done_num',"IF(task_status!=4,'上架','下架')"=>'task_status_str',"DATE_FORMAT(task_time, '%Y/%m/%d %H:%i:%s')"=>'task_time'];
        $field[] = 'task_active';
        $field[] = 'task_active_status';
        $field[] = 'task_active_reason';
        $field[] = 'task_share_num';
        $field[] = 'task_share_click';
        $field[] = 'task_super';
        $field[] = 'task_super_reason';
        $map['task_s_id'] = session('shop_id');
        $data = Db::name('task')
            ->where($map)
            ->field($field)
            ->order('task_time desc')
            ->paginate(25)
            ->each(function($item, $key){
                if($item['task_sheng'] == 0){
                    $item['task_sheng'] = '全国';
                }else{
                    $city = Db::name('area')
                        ->whereIn('district_id',$item['task_sheng'])
                        ->column ('district');
                    $item['task_sheng'] = implode (',',$city);
                }
                return $item;
            });
        //----------------------------
        $types = ['全部', '下载', '求赞求粉','问卷','投票','视频','分享'];
        $statuss = ['全部', '上架', '下架'];
        $actsss = ['全部任务', '普通任务', '活动任务', '达人任务'];
        //----------------------------
        return $this->fetch ('task/home_page',['data'=>$data,'pages'=>$pages,'search'=>$search,'types'=>$types,'statuss'=>$statuss,'actsss'=>$actsss]);
    }


    /**
     * 获取当前商户的审核状态
     * @return \think\response\Json
     */
    public function getShopSta()
    {
        $shop = Db::name('shops')
            ->where('s_id',session('shop_id'))
            ->field('s_status,s_money')
            ->find();
        $shopstatus = $shop['s_status'];
        if($shopstatus == 1){
            return json(['status'=>0,'msg'=>'认证审核中！']);
        }elseif ($shopstatus == 2){
            if($shop['s_money'] <= 0){
                return json(['status'=>0,'msg'=>'您平台币余额为0，请先充值！']);
            }
            return json(['status'=>200,'msg'=>'ok']);
        }elseif ($shopstatus == 3){
            return json(['status'=>0,'msg'=>'您的认证审核被拒绝，请检查后重新提交审核']);
        }else{
            return json(['status'=>0,'msg'=>'您还没有认证，请先去认证']);
        }
    }
    /**
     * 敏感词
     */
    public function sensitive()
    {
        $content1 = $_POST['content1'];
        $content2 = $_POST['content2'];
        $content3 = $_POST['content3'];
        $words = Db::table('up_sensitive')->column('s_word');
        if($words){
            foreach ($words as $k=>$v){
                if(strstr($content1,$v)){
                    return json(['status'=>0,'msg'=>'任务名称有敏感词，发布需要审核，确定吗？']);
                    exit();
                }
            }
            foreach ($words as $k=>$v){
                if(strstr($content2,$v)){
                    return json(['status'=>0,'msg'=>'任务简介有敏感词，发布需要审核，确定吗？']);
                    exit();
                }
            }
            foreach ($words as $k=>$v){
                if(strstr($content3,$v)){
                    return json(['status'=>0,'msg'=>'分享任务的内容有敏感词，发布需要审核，确定吗？']);
                    exit();
                }
            }
            return json(['status'=>200,'msg'=>'ok']);
        }else{
            return json(['status'=>200,'msg'=>'ok']);
        }
    }
    /**
     * 发布任务和提交
     * @return mixed|\think\response\Json
     */
    public function releaseTask(){
        extract(input());
        $shop = Db::name('shops')
            ->where('s_id',session('shop_id'))
            ->field(['s_id','s_money','s_status','s_name','s_thumb','s_image','s_jianjie','s_active_status','s_active_num'])
            ->find();

        if(request ()->isPost ()){
            if($shop['s_status'] != 2){
                return json(['status'=>100,'msg'=>'未认证实名认证，去认证']);
            }
            if($shop['s_money'] <= 0 || $money > $shop['s_money'] || round($task_num*$task_price,2) > $shop['s_money']){
                return json(['status'=>100,'msg'=>'金额不足，请充值']);
            }
            //*********活动任务的资格和次数 start ********
            if($task_active == 2){
                if($shop['s_active_status'] != 2){
                    return json(['status'=>100,'msg'=>'您还没有发布活动任务的权限，请去认证！']);
                }
                $ac_times = Db::table('up_task')
                    ->where('task_s_id',session('shop_id'))
                    ->where('task_active',2)
                    ->where('task_status','<>',5) // 不判断活动状态了，都算上，只有是没有删除
                    ->whereTime('task_time','month')
                    ->count();
                if($ac_times >= $shop['s_active_num']){
                    return json(['status'=>100,'msg'=>'您本月可发布活动任务的次数已超出限制']);
                }
            }
            //*********活动任务的资格和次数  end  ********
            if($task_type == 1){
                $res = $this->forTaskFabuCheck($task_type,$task_sheng,$task_shi,$task_qu,$task_num,$task_price,$task_name,$task_starttime,$task_endtime,$task_content,$task_href,$task_href_ios,$task_app_ios,$task_app_android);
            }else{
                $res = $this->forTaskFabuCheck($task_type,$task_sheng,$task_shi,$task_qu,$task_num,$task_price,$task_name,$task_starttime,$task_endtime,$task_content,$task_href);
            }
            if($res['status'] != 200){
                return json($res);
            }
            //----------------处理省市区的数据----start------
            $task_sheng = $task_sheng==-1 ? 0 : $task_sheng;
            if($task_shi == -1){
                $citys = Db::table('up_area')
                    ->where('level',2)
                    ->where('pid',$task_sheng)
                    ->column('district_id');
                $qus = Db::table('up_area')
                    ->where('level',3)
                    ->where('pid','in',$citys)
                    ->column('district_id');
                if($qus){
                    $task_qu = implode($qus,',');
                }else{
                    $task_qu = '';
                }
            }else{
                if($task_qu == -1){
                    $qus = Db::table('up_area')
                        ->where('level',3)
                        ->where('pid',$task_shi)
                        ->column('district_id');
                    if($qus){
                        $task_qu = implode($qus,',');
                    }else{
                        $task_qu = '';
                    }
                }else{
                    $task_qu = trim($task_qu,',');
                }
            }
            $task_shi = $task_shi==-1 ? 0 : $task_shi;
            //----------------处理省市区的数据----end------
            //***********************************************************************
            if(!$task_image){
                return json(['status'=>100,'msg'=>'请上传图片']);
            }
            //***********************************************************************
            $param = [
                'task_s_id'=>session('shop_id'),
                'task_number'=>GetRandCode(3).time(),//任务编号
                'task_type'=>$task_type,//任务类型
                'task_active'=>$task_active,//普通或者活动
                'task_href'=>$task_href,//除了视频（视频的话代表视频的链接地址），其他都有此值
                'task_sheng'=>$task_sheng,//省
                'task_shi'=>$task_shi,//市
                'task_qu'=>$task_qu,//区
                'task_num'=>$task_num,//任务数量
                'task_price'=>$task_price,//单个任务金额
                'task_money'=>$money,//任务总金额
                'task_name'=>$task_name,//任务名称
                'task_image'=>$task_image,//任务图片
                'task_starttime'=>$task_starttime,//开始时间
                'task_endtime'=>$task_endtime,//结束时间
                'task_content'=>$task_content,//任务简介
                'task_time'=>date('Y-m-d H:i:s'),
                'task_update_time'=>date('Y-m-d H:i:s'),
                'task_date'=>date('Y-m-d'),
                'task_hour'=>date('Y-m-d H'),
            ];
            //*************************************************************
            if($task_type == 1){
                $param['task_href_ios'] = $task_href_ios;
                $param['task_app_ios'] = $task_app_ios;
                $param['task_app_android'] = $task_app_android;
            }
            //活动任务是待审核的
            if($task_active == 2){
                $param['task_active_status'] = 1;
            }
            if(isset($mingantype) && $mingantype == 1){
                $param['task_active_status'] = 1;
            }
            if($task_type == 5){
                if(empty($task_video)){
                    return json(['status'=>100,'msg'=>'请上传视频']);
                }else{
                    $param['task_href'] = $task_video;
                }

            }

            //*************************************************************
            switch($task_type){
                case 1:
                    $mission = '下载';
                    break;
                case 2:
                    $mission = '求赞求粉';
                    break;
                case 3:
                    $mission = '问卷';
                    break;
                case 4:
                    $mission = '投票';
                    break;
                case 5:
                    $mission = '视频';
                    break;
                case 6:
                    $mission = '分享';
                    break;
            }

            if ($task_type == 6) {//6分享
                $param['task_share_type'] = $task_share_type;
                $empty_for_qudao = true;
                if ($task_share_type == 1) {
                    $param['task_share_submit'] = $task_share_submit; //要不要审核
                    $param['task_share_b_num'] = $task_share_b_num; //分享次数
                } else {
                    $param['task_share_d_fen'] = $task_share_d_fen; //分不分步骤
                    if ($task_share_d_fen == 1) {
                        $param['task_share_submit'] = $task_share_submit; //要不要审核
                    } else {
                        $param['task_share_timeout'] = $task_share_timeout; //超时时间
                        $empty_for_qudao = false;
                    }
                }
                $param['task_share_content_type'] = $task_share_content_type;
                if ($task_share_content_type == 1) {
                    if ($task_share_content == '<p><br></p>' || $task_share_content == '<p></p><p><br></p>') {
                        $res['status'] = 0;
                        $res['msg'] = '分享任务的内容不能为空';
                        return json($res);
                    }
                    $param['task_share_content'] = $task_share_content;
                } elseif ($task_share_content_type == 2){
                    if ($task_share_href == '') {
                        $res['status'] = 0;
                        $res['msg'] = '分享任务的链接不能为空';
                        return json($res);
                    }
                    $param['task_share_href'] = $task_share_href;
                }else{
                    if(!$task_image2) {
                        $res['status'] = 0;
                        $res['msg'] = '分享任务的分享图片不能为空';
                        return json($res);
                    }
                    $param['task_share_wechatpic_old'] = $task_image2;
                    $param['task_share_wechatpic_new'] = $this->hecheng_pic($task_image2);
                }
                if ($empty_for_qudao == true) {
                    if (empty($task_share_qudao)) {
                        return json(['status' => 100, 'msg' => '分享任务的渠道不能为空！']);
                    } else {
                        $param['task_share_qudao'] = implode($task_share_qudao, ',');
                    }
                }

            }
            Db::startTrans ();
            try{
                $task_id = Db::name('task')->insertGetId($param);
                if ($task_type == 6) {
                    if($task_share_type == 2){
                        if($task_share_d_fen == 2){
                            //TODO：分步骤的表(注意上面的总金额，和其他不一样)
                            $up_task_fen = trim($up_task_fen, '|');
                            $up_task_fen_arr = explode('|', $up_task_fen);
                            if (count($up_task_fen_arr) <= 0 || count($up_task_fen_arr) > 5) {
                                $res['status'] = 0;
                                $res['msg'] = '分步骤有误，请刷新重试';
                                return json($res);
                            } else {
                                Db::name('task_fen')->where('tf_task_id',$task_id)->delete();
                                $new_price = 0;
                                foreach ($up_task_fen_arr as $k => $v) {
                                    $fen_son = explode('_', $v);
                                    $insert_son = array();
                                    $insert_son['tf_task_id'] = $task_id;
                                    $insert_son['tf_order'] = $k+1;
                                    $insert_son['tf_desc'] = $fen_son[0];
                                    $insert_son['tf_submit'] = $fen_son[1];
                                    $insert_son['tf_money'] = $fen_son[2];
                                    $insert_son['tf_qudao'] = $fen_son[3];
                                    Db::name('task_fen')->insert($insert_son);
                                    $new_price += $fen_son[2];
                                }
                                $param['task_price'] = $new_price;
                                $param['task_money'] = $new_price*$task_num;
                                if($money != $param['task_money']){
                                    Db::name('task')->where('task_id',$task_id)->update([
                                        'task_price' =>$param['task_price'],
                                        'task_money' =>$param['task_money']
                                    ]);
                                }
                                $money = $new_price*$task_num;
                                if($shop['s_money'] <= 0 || $param['task_money'] > $shop['s_money']){
                                    Db::rollback ();
                                    return json(['status'=>100,'msg'=>'金额不足，请充值']);
                                }
                            }
                        }
                    }
                }
                Db::name('shops')->where('s_id', session('shop_id'))->setDec('s_money', $money);
                Db::name('shops')->where('s_id', session('shop_id'))->setInc('s_task_num', 1);
                //入商户明细和商户消息
                allInShopDetail($money, 1, session('shop_id'), '发布' . $mission . '任务');
                allInNotice('扣款', '您发布的' . $mission . '任务，成功扣款' . ($money / 10) . '元！', 3, session('shop_id'));
                Db::commit();
                return json(['status' => 200, 'msg' => '发布成功']);
            }catch (\Exception $e){
                Db::rollback ();
                return json(['status'=>100,'msg'=>'发布失败']);
            }
        }
        if($shop['s_name'] == '' || $shop['s_thumb'] == '' || $shop['s_image'] == '' || $shop['s_jianjie'] == ''){
            $perfect = 'no';//未完善信息
        }else{
            $perfect = 'yes';
        }
        //省
        $pros = Db::table('up_area')
            ->where('level',1)
            ->field('district_id,district')
            ->select();
        if($type == 1){
            return $this->fetch('task/release_task',['perfect'=>$perfect,'pros'=>$pros]);
        }else{
            return $this->fetch('task/release_task_fen',['perfect'=>$perfect,'pros'=>$pros]);
        }

    }
    /**
     * 发布达人任务和提交
     * @return mixed|\think\response\Json
     */
    public function releaseDarenTask(){
        extract(input());
        $shop = Db::name('shops')
            ->where('s_id',session('shop_id'))
            ->field(['s_id','s_money','s_status','s_name','s_thumb','s_image','s_jianjie','s_active_status','s_active_num'])
            ->find();

        if(request ()->isPost ()){
            if($shop['s_status'] != 2){
                return json(['status'=>100,'msg'=>'未认证实名认证，去认证']);
            }
            if($shop['s_money'] <= 0 || $task_price > $shop['s_money']){
                return json(['status'=>100,'msg'=>'金额不足，请充值']);
            }
            //*********活动任务的资格和次数 start ********
            if($task_active != 3){
                return json(['status'=>100,'msg'=>'这里只能发布达人任务！']);
            }
            //*********活动任务的资格和次数  end  ********

            //***********************************************************************
            if(!$task_image){
                return json(['status'=>100,'msg'=>'请上传图片']);
            }
            //***********************************************************************
            $param = [
                'task_s_id'=>session('shop_id'),
                'task_number'=>GetRandCode(3).time(),//任务编号
                'task_type'=>$task_type,//任务类型
                'task_active'=>3,//普通或者活动
                'task_href'=>$task_href,//除了视频（视频的话代表视频的链接地址），其他都有此值
                'task_num'=>1,//任务数量
                'task_price'=>$task_price,//单个任务金额
                'task_money'=>$task_price,//任务总金额
                'task_name'=>$task_name,//任务名称
                'task_image'=>$task_image,//任务图片
                'task_starttime'=>$task_starttime,//开始时间
                'task_endtime'=>$task_endtime,//结束时间
                'task_content'=>$task_content,//任务简介
                'task_time'=>date('Y-m-d H:i:s'),
                'task_update_time'=>date('Y-m-d H:i:s'),
                'task_date'=>date('Y-m-d'),
                'task_hour'=>date('Y-m-d H'),
                'task_super'=>1,
            ];
            switch($task_type){
                case 1:
                    $mission = '下载';
                    break;
                case 2:
                    $mission = '求赞求粉';
                    break;
                case 3:
                    $mission = '问卷';
                    break;
                case 4:
                    $mission = '投票';
                    break;
                case 5:
                    $mission = '视频';
                    break;
                case 6:
                    $mission = '分享';
                    break;
            }

            if ($task_type == 6) {//6分享
                $param['task_share_type'] = $task_share_type;
                $empty_for_qudao = true;
                if ($task_share_type == 1) {
                    $param['task_share_b_num'] = $task_share_b_num; //分享次数
                }
                $param['task_share_content_type'] = $task_share_content_type;
                if ($task_share_content_type == 1) {
                    if ($task_share_content == '<p><br></p>' || $task_share_content == '<p></p><p><br></p>') {
                        $res['status'] = 0;
                        $res['msg'] = '分享任务的内容不能为空';
                        return json($res);
                    }
                    $param['task_share_content'] = $task_share_content;
                } elseif ($task_share_content_type == 2){
                    if ($task_share_href == '') {
                        $res['status'] = 0;
                        $res['msg'] = '分享任务的链接不能为空';
                        return json($res);
                    }
                    $param['task_share_href'] = $task_share_href;
                }else{
                    if(!$task_image2) {
                        $res['status'] = 0;
                        $res['msg'] = '分享任务的分享图片不能为空';
                        return json($res);
                    }
                    $param['task_share_wechatpic_old'] = $task_image2;
                    $param['task_share_wechatpic_new'] = $this->hecheng_pic($task_image2);
                }
                if ($empty_for_qudao == true) {
                    if (empty($task_share_qudao)) {
                        return json(['status' => 100, 'msg' => '分享任务的渠道不能为空！']);
                    } else {
                        $param['task_share_qudao'] = implode($task_share_qudao, ',');
                    }
                }

            }
            Db::startTrans ();
            try{
                $task_id = Db::name('task')->insertGetId($param);
                Db::name('shops')->where('s_id', session('shop_id'))->setDec('s_money', $task_price);
                Db::name('shops')->where('s_id', session('shop_id'))->setInc('s_task_num', 1);
                //入商户明细和商户消息
                allInShopDetail($task_price, 1, session('shop_id'), '发布达人任务' . $mission);
                allInNotice('扣款', '您发布的达人任务：' . $mission . '，成功扣款' . ($task_price / 10) . '元！', 3, session('shop_id'));
                Db::commit();
                return json(['status' => 200, 'msg' => '发布成功']);
            }catch (\Exception $e){
                Db::rollback ();
                return json(['status'=>100,'msg'=>'发布失败']);
            }
        }
        if($shop['s_name'] == '' || $shop['s_thumb'] == '' || $shop['s_image'] == '' || $shop['s_jianjie'] == ''){
            $perfect = 'no';//未完善信息
        }else{
            $perfect = 'yes';
        }
        return $this->fetch('task/release_daren_task',['perfect'=>$perfect]);

    }
    public function forTaskFabuCheck($task_type='',$task_sheng='',$task_shi='',$task_qu='',$task_num='',$task_price='',$task_name='',$task_starttime='',$task_endtime='',$task_content='',$task_href='',$task_href_ios='',$task_app_ios='',$task_app_android='')
    {
        $res = [
            'status'=>200,
            'msg'=>'OK'
        ];
        if(empty($task_type)){
            $res['status'] = 0;
            $res['msg'] = '任务类型不能为空';
            return $res;
        }
        if($task_type != 5 && $task_type != 6){
            if(empty($task_href)){
                $res['status'] = 0;
                $res['msg'] = '任务链接不能为空';
                return $res;
            }
        }
        if($task_type == 1){
            if(!$task_href_ios || !$task_app_ios || !$task_app_android){
                $res['status'] = 0;
                $res['msg'] = '任务各下载链接不能为空';
                return $res;
            }
        }
        if(empty($task_sheng)){//试试-1是不是为空
            $res['status'] = 0;
            $res['msg'] = '请选择任务区域';
            return $res;
        }else{
            if($task_sheng != -1){
                if(empty($task_shi)){
                    $res['status'] = 0;
                    $res['msg'] = '请选择任务所在市';
                    return $res;
                }else{
                    if($task_shi != -1){
                        if(empty($task_qu)){
                            $res['status'] = 0;
                            $res['msg'] = '请选择任务所在区';
                            return $res;
                        }
                    }

                }
            }
        }
        if(empty($task_num)){
            $res['status'] = 0;
            $res['msg'] = '任务总数不能为空';
            return $res;
        }
        if(empty($task_price)){
            $res['status'] = 0;
            $res['msg'] = '任务单个金额不能为空';
            return $res;
        }
        if(empty($task_name)){
            $res['status'] = 0;
            $res['msg'] = '任务名称不能为空';
            return $res;
        }
        if(empty($task_starttime) || empty($task_endtime)){
            $res['status'] = 0;
            $res['msg'] = '任务开始时间或结束时间不能为空';
            return $res;
        }else{
            $today = date('Y-m-d H:i:s');
            if($task_starttime <= $today){
                $res['status'] = 0;
                $res['msg'] = '任务开始时间不能小于当前时间';
                return $res;
            }
            $cb = (strtotime($task_endtime) - strtotime($task_starttime))/3600;
            if($cb < 48){
                $res['status'] = 0;
                $res['msg'] = '任务开始时间不能大于结束时间且相隔不能低于48小时';
                return $res;
            }
        }
        if($task_content == '<p><br></p>' || $task_content == '<p></p><p><br></p>'){
            $res['status'] = 0;
            $res['msg'] = '任务简介不能为空';
            return $res;
        }
        return $res;
    }

    /**
     * 编辑之前判断能不能编辑
     * @return \think\response\Json
     */
    public function taskEditCan()
    {
        $id = $_POST['id'];
        $data = Db::table('up_task')
            ->where('task_id',$id)
            ->field('task_status,task_share_d_fen,task_super,task_active')
            ->find();
        $shopstatus = $data['task_status'];
        if($shopstatus){
            if($data['task_share_d_fen'] == 2 && $shopstatus != 1){
                return json(['status'=>0,'msg'=>'分步骤任务只能在未开始的时候才可以编辑！']);
            }
            if($data['task_super'] != 3 && $data['task_active'] == 3){
                return json(['status'=>0,'msg'=>'只有被拒绝的达人任务才可以编辑！']);
            }
            if($shopstatus == 3){
                return json(['status'=>0,'msg'=>'任务已结束！']);
            }elseif ($shopstatus == 4){
                return json(['status'=>0,'msg'=>'任务已下架！']);
            }elseif ($shopstatus == 5){
                return json(['status'=>0,'msg'=>'任务已不存在！']);
            }else{
                return json(['status'=>200,'msg'=>'ok']);
            }
        }else{
            return json(['status'=>0,'msg'=>'此任务不存在！请刷新重试！']);
        }

    }
    /**
     * 任务编辑 （若任务总金额变化，多退少补）
     * @return mixed|\think\response\Json
     */
    public function editTask(){
        extract(input());
        $tasktype = ['error','下载','求赞求粉','问卷','投票','视频','分享'];
        if(request ()->isPost ()){
            $shop = Db::name('shops')
                ->where('s_id',session('shop_id'))
                ->field(['s_id','s_money','s_status','s_name','s_thumb','s_image','s_jianjie','s_active_num'])
                ->find();
            $tasks = Db::name('task')
                ->where('task_id',$task_id)
                ->field (['task_type','task_price','task_get_num','task_num','task_money','task_status','task_active','task_active_status','task_share_d_fen','task_share_wechatpic_old'])
                ->find();
            if($tasks['task_status'] != 1 && $tasks['task_status'] != 2){
                return json(['status'=>100,'msg'=>'此任务不可编辑，请退出！']);
            }
            if($tasks['task_status'] != 1 && $tasks['task_share_d_fen'] == 2){
                return json(['status'=>100,'msg'=>'此任务不可编辑，请退出！']);
            }
            if($tasks['task_share_d_fen'] == 2){
                if(empty($task_starttime) || empty($task_endtime)){
                    $res['status'] = 0;
                    $res['msg'] = '任务开始时间或结束时间不能为空';
                    return json($res);
                }else{
                    $today = date('Y-m-d H:i:s');
                    if($task_starttime <= $today){
                        $res['status'] = 0;
                        $res['msg'] = '任务开始时间不能小于当前时间';
                        return json($res);
                    }
                    $cb = (strtotime($task_endtime) - strtotime($task_starttime))/3600;
                    if($cb < 48){
                        $res['status'] = 0;
                        $res['msg'] = '任务开始时间不能大于结束时间且相隔不能低于48小时';
                        return json($res);
                    }
                }
                return json(['status'=>100,'msg'=>'此任务不可编辑，请退出！']);
            }
            //****-------活动任务------------
            if($tasks['task_active'] == 2){
                $ac_times = Db::table('up_task')
                    ->where('task_s_id',session('shop_id'))
                    ->where('task_active',2)
                    ->where('task_status','<>',5) // 不判断活动状态了，都算上，只有是没有删除
                    ->whereTime('task_time','month')
                    ->count();
                if($ac_times > $shop['s_active_num']){
                    return json(['status'=>100,'msg'=>'您本月可发布活动任务的次数已超出限制']);
                }
            }

            //****-------活动任务------------
            //-----------任务简介和内容的编辑器先不做判断------------
            if(!$task_num || !$task_image || !$task_name){
                return json(['status'=>100,'msg'=>'有未填项']);
            }
            if($task_type != 5 && $task_type != 6 && !$task_href){
                return json(['status'=>100,'msg'=>'任务链接不能为空']);
            }
            if($task_type == 5 && !$task_video){
                return json(['status'=>100,'msg'=>'视频不能为空']);
            }
            if($task_type == 1){
                if(!$task_href_ios || !$task_app_ios || !$task_app_android){
                    return json(['status'=>100,'msg'=>'任务各下载链接不能为空']);
                }
            }
            //--------------------------
            if($task_num_before != $tasks['task_num']){
                if($task_num <= $tasks['task_get_num']){
                    return json(['status'=>100,'msg'=>'任务总数不可少于已领取数量']);
                }
            }
            if($money != $tasks['task_money']){
                if($money > $tasks['task_money']){//增加了总金额
                    if($shop['s_money'] <= 0 || $money-$tasks['task_money'] > $shop['s_money']){
                        return json(['status'=>100,'msg'=>'金额不足，请充值']);
                    }
                }
            }
            $param = [
                'task_num'=>$task_num,//任务数量
                'task_money'=>$money,//任务总金额
                'task_name'=>$task_name,//任务名称
                'task_image'=>$task_image,//任务图片
                'task_content'=>$task_content,//任务简介
                'task_starttime'=>$task_starttime,//开始时间（每一次都更新下时间，是为了分步骤考虑，有可能变）
                'task_endtime'=>$task_endtime,//结束时间
                'task_update_time'=>date('Y-m-d H:i:s')
            ];
            if($task_type == 1){
                $param['task_href_ios'] = $task_href_ios;
                $param['task_app_ios'] = $task_app_ios;
                $param['task_app_android'] = $task_app_android;
            }
            //活动任务只要编辑就都是待审核的(改为只有审核失败的编辑后才会改)
            if($tasks['task_active'] == 2){
                if($tasks['task_active_status'] == 3){
                    $param['task_active_status'] = 1;
                }
            }
            //普通任务只要有敏感词编辑就都是待审核的
            if($tasks['task_active'] == 1){
                $param['task_active_status'] = 2;
            }
            if(isset($mingantype) && $mingantype == 1){
                $param['task_active_status'] = 1;
            }
            if($task_type == 5 && $task_video){
                $param['task_href'] = $task_video;
            }
            if($task_type != 5){
                $param['task_href'] = $task_href;
            }

//
            Db::startTrans ();
            try{
                if ($task_type == 6) {//6分享
                    $empty_for_qudao = true;
                    if ($task_share_type == 1) {
                        $param['task_share_submit'] = $task_share_submit; //要不要审核
                        $param['task_share_b_num'] = $task_share_b_num; //分享次数
                    } else {
                        $param['task_share_d_fen'] = $task_share_d_fen; //分不分步骤
                        if ($task_share_d_fen == 1) {
                            $param['task_share_submit'] = $task_share_submit; //要不要审核
                        } else {
                            $param['task_share_timeout'] = $task_share_timeout; //超时时间
                            if(empty($task_share_timeout) || $task_share_timeout <= 0){
                                $res['status'] = 0;
                                $res['msg'] = '每步完成时间不能为空';
                                return json($res);
                            }
                            $empty_for_qudao = false;
//                        开始重新计算
                            $up_task_fen = trim($up_task_fen, '|');
                            $up_task_fen_arr = explode('|', $up_task_fen);
                            if (count($up_task_fen_arr) <= 0 || count($up_task_fen_arr) > 5) {
                                $res['status'] = 0;
                                $res['msg'] = '分步骤有误，请刷新重试';
                                return json($res);
                            } else {
                                Db::name('task_fen')->where('tf_task_id', $task_id)->delete();
                                $new_price = 0;
                                foreach ($up_task_fen_arr as $k => $v) {
                                    $fen_son = explode('_', $v);
                                    $insert_son = array();
                                    $insert_son['tf_task_id'] = $task_id;
                                    $insert_son['tf_order'] = $k + 1;
                                    $insert_son['tf_desc'] = $fen_son[0];
                                    $insert_son['tf_submit'] = $fen_son[1];
                                    $insert_son['tf_money'] = $fen_son[2];
                                    $insert_son['tf_qudao'] = $fen_son[3];
                                    Db::name('task_fen')->insert($insert_son);
                                    $new_price += $fen_son[2];
                                }
                                $param['task_price'] = $new_price;
                                $param['task_money'] = $new_price * $task_num;
                                $money = $new_price * $task_num;
                                if ($money != $tasks['task_money']) {
                                    if ($money > $tasks['task_money']) {//增加了总金额
                                        if ($shop['s_money'] <= 0 || $money - $tasks['task_money'] > $shop['s_money']) {
                                            Db::rollback ();
                                            return json(['status' => 100, 'msg' => '金额不足，请充值']);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    $param['task_share_content_type'] = $task_share_content_type;
                    if ($task_share_content_type == 1) {
                        if ($task_share_content == '<p><br></p>' || $task_share_content == '<p></p><p><br></p>') {
                            $res['status'] = 0;
                            $res['msg'] = '分享任务的内容不能为空';
                            return json($res);
                        }
                        $param['task_share_content'] = $task_share_content;
                    } elseif ($task_share_content_type == 2) {
                        if ($task_share_href == '') {
                            $res['status'] = 0;
                            $res['msg'] = '分享任务的链接不能为空';
                            return json($res);
                        }
                        $param['task_share_href'] = $task_share_href;
                    }else{
                        if(!$task_image2) {
                            $res['status'] = 0;
                            $res['msg'] = '分享任务的分享图片不能为空';
                            return json($res);
                        }
                        if($tasks['task_share_wechatpic_old'] != $task_image2){
                            $param['task_share_wechatpic_old'] = $task_image2;
                            $param['task_share_wechatpic_new'] = $this->hecheng_pic($task_image2);
                        }

                    }
                    if ($empty_for_qudao == true) {
                        if (empty($task_share_qudao)) {
                            return json(['status' => 100, 'msg' => '分享任务的渠道不能为空！']);
                        } else {
                            $param['task_share_qudao'] = implode($task_share_qudao, ',');
                        }
                    }

                }

                Db::name('task')->where('task_id', $task_id)->update($param);
                if ($money != $tasks['task_money']) {
                    if ($money > $tasks['task_money']) {//增加了总金额
                        Db::name('shops')->where('s_id', session('shop_id'))->setDec('s_money', $money - $tasks['task_money']);//补上增加的钱
                        //入商户明细和商户消息
                        allInShopDetail($money - $tasks['task_money'], 1, session('shop_id'), '编辑扣除' . $tasktype[$tasks['task_type']] . '任务差值');
                        allInNotice('扣款', '您编辑的' . $tasktype[$tasks['task_type']] . '任务，成功扣款差值' . (($money - $tasks['task_money']) / 10) . '元！', 3, session('shop_id'));
                    } else {
                        //减少总金额
                        Db::name('shops')->where('s_id', session('shop_id'))->setInc('s_money', $tasks['task_money'] - $money);//退还多的钱
                        //入商户明细和商户消息
                        allInShopDetail($tasks['task_money'] - $money, 2, session('shop_id'), '编辑返还' . $tasktype[$tasks['task_type']] . '任务差值');
                        allInNotice('返还', '您编辑的' . $tasktype[$tasks['task_type']] . '任务，成功返还差值' . (($tasks['task_money'] - $money) / 10) . '元！', 3, session('shop_id'));
                    }
                }
                Db::commit();
                return json(['status' => 200, 'msg' => '修改成功']);
            }catch (\Exception $e){
                Db::rollback ();
                return json(['status'=>100,'msg'=>'发布失败']);
            }
        }
        $data = Db::name('task')
            ->where('task_id',$task_id)
            ->find();
        if($data['task_sheng'] == 0){
            $data['task_sheng'] = '全国';
        }else{
            $data['task_sheng'] = Db::table('up_area')
                ->where('district_id',$data['task_sheng'])
                ->value('district');
        }
        if($data['task_shi'] == 0){
            $data['task_shi'] = '全部';
        }else{
            $data['task_shi'] = Db::table('up_area')
                ->where('district_id',$data['task_shi'])
                ->value('district');
        }

        if($data['task_sheng'] != '全国' && $data['task_shi'] != '全部'){
            $qus = Db::table('up_area')
                ->where('district_id','in',$data['task_qu'])
                ->column('district');
            $data['task_qu'] = implode($qus,' ');
        }else{
            $data['task_qu'] = '全部';
        }
        if($data['task_share_qudao']){
            $data['task_share_qudao'] = explode(',',$data['task_share_qudao']);
        }
//
        if ($data['task_type'] == 6) {
            if($data['task_share_type'] == 2){
                if($data['task_share_d_fen'] == 2){
                    $up_task_fen = Db::table('up_task_fen')
                        ->where('tf_task_id',$task_id)
                        ->field('tf_desc,tf_submit,tf_money,tf_qudao,tf_order')
                        ->order('tf_order')
                        ->select();
                    $form_data = array();
                    $max = 1;
                    foreach ($up_task_fen as $k=>$v){
                        $max = $v['tf_order'];
                        $form_data['five_' . $v['tf_order']] = $v['tf_desc'] . '_' . $v['tf_submit'] . '_' . $v['tf_money'] . '_' . $v['tf_qudao'];
                    }
                    return $this->fetch('task/edit_task',
                        ['data'=>$data,'tasktype'=>$tasktype,'up_task_fen'=>$up_task_fen,'form_data'=>json_encode($form_data),'max'=>$max]);
                }
            }
        }
        if($data['task_active'] == 3){
            return $this->fetch('task/edit_task_daren',['data'=>$data,'tasktype'=>$tasktype,'form_data'=>'']);
        }else{
            return $this->fetch('task/edit_task',['data'=>$data,'tasktype'=>$tasktype,'form_data'=>'']);
        }
    }
    /**
     * 达人任务编辑 （若任务总金额变化，多退少补）
     * @return mixed|\think\response\Json
     */
    public function editTaskDaren(){
        extract(input());
        $tasktype = ['error','下载','求赞求粉','问卷','投票','视频','分享'];
        if(request ()->isPost ()){
            $shop = Db::name('shops')
                ->where('s_id',session('shop_id'))
                ->field(['s_id','s_money','s_status','s_name','s_thumb','s_image','s_jianjie','s_active_num'])
                ->find();
            $tasks = Db::name('task')
                ->where('task_id',$task_id)
                ->field (['task_type','task_price','task_get_num','task_num','task_money','task_status','task_active','task_super','task_share_d_fen','task_share_wechatpic_old'])
                ->find();
            if($tasks['task_status'] != 1){
                return json(['status'=>100,'msg'=>'此任务不可编辑，请退出！']);
            }
            //-----------任务简介和内容的编辑器先不做判断------------
            if(!$task_num || !$task_image || !$task_name){
                return json(['status'=>100,'msg'=>'有未填项']);
            }
            if($task_type != 5 && $task_type != 6 && !$task_href){
                return json(['status'=>100,'msg'=>'任务链接不能为空']);
            }
            //--------------------------
            if($money != $tasks['task_money']){
                if($money > $tasks['task_money']){//增加了总金额
                    if($shop['s_money'] <= 0 || $money-$tasks['task_money'] > $shop['s_money']){
                        return json(['status'=>100,'msg'=>'金额不足，请充值']);
                    }
                }
            }
            $param = [
                'task_price'=>$money,//任务总金额
                'task_money'=>$money,//任务总金额
                'task_name'=>$task_name,//任务名称
                'task_image'=>$task_image,//任务图片
                'task_content'=>$task_content,//任务简介
                'task_starttime'=>$task_starttime,//开始时间（每一次都更新下时间，是为了分步骤考虑，有可能变）
                'task_endtime'=>$task_endtime,//结束时间
                'task_update_time'=>date('Y-m-d H:i:s')
            ];

            //活动任务只要编辑就都是待审核的(改为只有审核失败的编辑后才会改)
            if($tasks['task_active'] == 3){
                if($tasks['task_super'] == 3){
                    $param['task_super'] = 1;
                }
            }
            if($task_type != 5){
                $param['task_href'] = $task_href;
            }

//
            Db::startTrans ();
            try{
                if ($task_type == 6) {//6分享
                    $empty_for_qudao = true;
                    if ($task_share_type == 1) {
                        $param['task_share_b_num'] = $task_share_b_num; //分享次数
                    }
                    $param['task_share_content_type'] = $task_share_content_type;
                    if ($task_share_content_type == 1) {
                        if ($task_share_content == '<p><br></p>' || $task_share_content == '<p></p><p><br></p>') {
                            $res['status'] = 0;
                            $res['msg'] = '分享任务的内容不能为空';
                            return json($res);
                        }
                        $param['task_share_content'] = $task_share_content;
                    } elseif ($task_share_content_type == 2) {
                        if ($task_share_href == '') {
                            $res['status'] = 0;
                            $res['msg'] = '分享任务的链接不能为空';
                            return json($res);
                        }
                        $param['task_share_href'] = $task_share_href;
                    }else{
                        if(!$task_image2) {
                            $res['status'] = 0;
                            $res['msg'] = '分享任务的分享图片不能为空';
                            return json($res);
                        }
                        if($tasks['task_share_wechatpic_old'] != $task_image2){
                            $param['task_share_wechatpic_old'] = $task_image2;
                            $param['task_share_wechatpic_new'] = $this->hecheng_pic($task_image2);
                        }
                    }
                    if ($empty_for_qudao == true) {
                        if (empty($task_share_qudao)) {
                            return json(['status' => 100, 'msg' => '分享任务的渠道不能为空！']);
                        } else {
                            $param['task_share_qudao'] = implode($task_share_qudao, ',');
                        }
                    }

                }

                Db::name('task')->where('task_id', $task_id)->update($param);
                if ($money != $tasks['task_money']) {
                    if ($money > $tasks['task_money']) {//增加了总金额
                        Db::name('shops')->where('s_id', session('shop_id'))->setDec('s_money', $money - $tasks['task_money']);//补上增加的钱
                        //入商户明细和商户消息
                        allInShopDetail($money - $tasks['task_money'], 1, session('shop_id'), '编辑扣除' . $tasktype[$tasks['task_type']] . '达人任务差值');
                        allInNotice('扣款', '您编辑的' . $tasktype[$tasks['task_type']] . '达人任务，成功扣款差值' . (($money - $tasks['task_money']) / 10) . '元！', 3, session('shop_id'));
                    } else {
                        //减少总金额
                        Db::name('shops')->where('s_id', session('shop_id'))->setInc('s_money', $tasks['task_money'] - $money);//退还多的钱
                        //入商户明细和商户消息
                        allInShopDetail($tasks['task_money'] - $money, 2, session('shop_id'), '编辑返还' . $tasktype[$tasks['task_type']] . '达人任务差值');
                        allInNotice('返还', '您编辑的' . $tasktype[$tasks['task_type']] . '达人任务，成功返还差值' . (($tasks['task_money'] - $money) / 10) . '元！', 3, session('shop_id'));
                    }
                }
                Db::commit();
                return json(['status' => 200, 'msg' => '修改成功']);
            }catch (\Exception $e){
                Db::rollback ();
                return json(['status'=>100,'msg'=>'发布失败']);
            }
        }
    }


    /**
     * 基本信息（详情）
     * @return mixed
     */
    public function baseMessage(){
        $field = ['task_id','task_s_id','task_number','task_name','task_image','task_type','task_href',
            'task_share_type','task_sheng','task_shi','task_qu','task_num','task_price','task_money','task_get_num',
            'task_done_num',"DATE_FORMAT(task_starttime, '%Y-%m-%d %H:%i')"=>'task_starttime',
            "DATE_FORMAT(task_endtime, '%Y-%m-%d %H:%i')"=>'task_endtime','task_content','task_share_content',
            'task_status',"IF(task_num!=0,CONCAT(left((task_done_num/task_num)*100,5),'%'),'0%')"=>'complete'];
        $field[] = 'task_href_ios';
        $field[] = 'task_app_ios';
        $field[] = 'task_app_android';
        $field[] = 'task_share_content_type';
        $field[] = 'task_share_href';
        $field[] = 'task_active';
        $field[] = 'task_active_status';
        $field[] = 'task_active_reason';
        $field[] = 'task_share_num';
        $field[] = 'task_share_click';
        $field[] = 'task_share_qudao';
        $field[] = 'task_share_d_fen';
        $field[] = 'task_share_submit';
        $field[] = 'task_share_timeout';
        $field[] = 'task_super';
        $field[] = 'task_super_reason';
        $field[] = 'task_lock';
        $field[] = 'task_super_admin';
        $field[] = 'task_super_first';
        $field[] = 'task_super_lase';
        $field[] = 'task_share_wechatpic_old';
        $data = Db::name('task')
            ->where('task_id',input('task_id'))
            ->field($field)
            ->find();
        $quyu = '';
        if($data['task_sheng'] == 0){
            $quyu = '全国';
        }else{
            $quyu .= Db::table('up_area')
                ->where('district_id',$data['task_sheng'])
                ->value('district');
            if($data['task_shi'] == 0){
                $quyu .= '>全部';
            }else{
                $quyu .= '>' . Db::table('up_area')
                    ->where('district_id',$data['task_shi'])
                    ->value('district');
                if($data['task_sheng'] != 0 && $data['task_shi'] != 0){
                    $qus = Db::table('up_area')
                        ->where('district_id','in',$data['task_qu'])
                        ->column('district');
                    $quyu .= '>' . implode($qus,' ');
                }else{
                    $quyu .= '>全部';
                }
            }
        }
        $tasktype = ['error','下载','求赞求粉','问卷','投票','视频','分享'];
        //
        $sons = '';
        if($data['task_share_d_fen'] == 2){
            $sons = Db::table('up_task_fen')
                ->where('tf_task_id',$data['task_id'])
                ->order('tf_order ASC')
                ->select();
        }
        //
        $data['task_superman'] = '暂无';
        if($data['task_active'] == 3 && $data['task_super'] == 2){
            $tuuid = Db::table('up_task_user')
                ->where('tu_task_id',$data['task_id'])
                ->value('tu_user_id');
            $user_username = Db::table('up_u_user')
                ->where('user_id',$tuuid)
                ->value('user_username');
            $data['task_superman'] = $user_username;
        }
        return $this->fetch ('task/base_message',['data'=>$data,'quyu'=>$quyu,'tasktype'=>$tasktype,'sons'=>$sons]);
    }


    /**
     * 领取信息
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function receiveMessage(){
        extract(input());
        $page = input('param.page');
        if(isset($_REQUEST['task_id'])){
            $task_id = input('task_id');
            session('task_info_go_id',$task_id);
        }else{
            $task_id = session('task_info_go_id');
        }
        if(!$page){
            $pages = 0;
        }else{
            $pages = $page-1;
        }
        $field2 = [];
        $field2[] = 'task_type';
        $field2[] = 'task_share_type';
        $field2[] = 'task_share_num';
        $field2[] = 'task_share_click';
        $field2[] = 'task_share_d_fen';
        $field2[] = 'task_share_b_num';
        $tt = Db::name('task')->where('task_id',$task_id)->field($field2)->find();
        $field = ['tu_id','tu_task_id','tu_number','user_username','user_image',"DATE_FORMAT(tu_receive_time, '%Y-%m-%d %H:%i')"=>'tu_receive_time',"DATE_FORMAT(tu_submit_time, '%Y-%m-%d %H:%i')"=>'tu_submit_time','tu_task_active'];
        $field[] = 'tu_share_fen';
        $field[] = 'tu_share_b_num';

        $data = Db::name('task_user tu')
            ->join('u_user u','tu.tu_user_id=u.user_id','left')
            ->where(['tu_task_id'=>$task_id,'tu_status'=>['in','1,2,3,4'],'tu_del'=>1])
            ->field($field)
            ->order('tu_receive_time desc')
            ->paginate(25);
        $alls = 1;
        if($tt['task_share_d_fen'] == 2){
            $alls = Db::table('up_task_fen')
                ->where('tf_task_id',$task_id)
                ->count();
        }
        return $this->fetch ('task/receive_message',['data'=>$data,'pages'=>$pages,'task_id'=>$task_id,'alls'=>$alls,'tt'=>$tt]);
    }


    /**
     * 领取信息详情
     * @return mixed
     */
    public function receiveDetail(){
        $field = ['tu_id','tu_number','user_username','user_image',"DATE_FORMAT(tu_receive_time, '%Y-%m-%d %H:%i')"=>'tu_receive_time',"DATE_FORMAT(tu_submit_time, '%Y-%m-%d %H:%i')"=>'tu_submit_time','tu_status',"DATE_FORMAT(tu_do_time, '%Y-%m-%d %H:%i')"=>'tu_do_time','tu_image','tu_task_active'];
        $field[] = 'task_id';
        $field[] = 'task_type';
        $field[] = 'task_share_type';
        $field[] = 'task_share_num';
        $field[] = 'task_share_click';
        $field[] = 'task_share_d_fen';
        $field[] = 'task_share_b_num';
        $field[] = 'tu_share_fen';
        $field[] = 'tu_share_b_num';
        $field[] = 'task_active';
        $data = Db::name('task_user tu')
            ->join('task t','tu.tu_task_id=t.task_id','LEFT')
            ->join('u_user u','tu.tu_user_id=u.user_id','left')
            ->where(['tu_id'=>input('tu_id')])
            ->field ($field)
            ->find();
        $now_status = ['未知', '待审核', '已通过','已拒绝','已领取','已结束','已取消','已超时'];
        if($data['tu_image']){
            $data['tu_image'] = explode(',',$data['tu_image']);
        }
        $alls = 1;
        if($data['task_share_d_fen'] == 2){
            $alls = Db::table('up_task_fen')
                ->where('tf_task_id',$data['task_id'])
                ->count();
        }
        return $this->fetch ('task/receive_detail',['data'=>$data,'now_status'=>$now_status,'alls'=>$alls]);
    }

    /**
     * 下架任务
     * @return \think\response\Json
     */
    public function offLine(){
        extract(input());
        $tasktype = ['error','下载','求赞求粉','问卷','投票','视频','分享'];
        $task = Db::name('task')->where('task_id',$task_id)
            ->field ('task_s_id,task_type,task_num,task_price,task_money,task_done_num,task_status,task_active')
            ->find();
        if($task['task_status'] == 3 || $task['task_status'] == 4 || $task['task_status'] == 5){
            return json(['status'=>100,'msg'=>'此任务已结束或已经被下架']);
        }
        // 启动事务
        Db::startTrans();
        try{// 提交事务
            if($task['task_status'] == 1 || $task['task_status'] == 2){
                if($task['task_active'] == 3){
                    $tu_super_yes = Db::name('task_user')
                        ->where('tu_task_id',$task_id)
                        ->value('tu_super_yes');
                    if($tu_super_yes){
                        if($tu_super_yes == 2){
                            Db::commit();
                            return json(['status'=>100,'msg'=>'此达人任务已被接受，不可下架！']);
                        }else{
                            Db::name('task_user')->where('tu_task_id',$task_id)->delete();
                        }
                    }
                    $back = $task['task_price'];
                }else{
                    $back = ($task['task_num'] - $task['task_done_num'])*$task['task_price'];
                }
                if($back > 0){
                    Db::name('shops')
                        ->where('s_id',$task['task_s_id'])
                        ->setInc('s_money',$back);
                    //入商户明细和商户消息
                    allInShopDetail($back,2,session('shop_id'),$tasktype[$task['task_type']].'任务下架退还');
                    allInNotice('任务下架退还',$tasktype[$task['task_type']].'任务下架退还，返还'.($back/10).'元！',3,session('shop_id'));
                }
            }
            Db::name('task')->where('task_id',$task_id)->update (['task_status'=>4,'task_update_time'=>date('Y-m-d H:i:s')]);
            Db::commit();
            return json(['status'=>200,'msg'=>'下架成功']);
            //=====end===============
        } catch (\Exception $e) {
            Db::rollback();
            return json(['status'=>100,'msg'=>'下架失败']);
        }
    }


    /**
     * 删除任务（只要还有未完成的，不管你做了没有，都不能删除）（）
     * @return \think\response\Json
     */
    public function deleteTask(){
        extract(input());
        $task = Db::name('task')->where('task_id',$task_id)
            ->field ('task_s_id,task_num,task_price,task_money,task_done_num,task_status')
            ->find();
        if($task['task_status'] != 4){
            return json(['status'=>100,'msg'=>'只有已下架的才能删除']);
        }else{
            $have = Db::name('task_user')
                ->where('tu_task_id',$task_id)
                ->where('tu_status','in','1,3,4')//如果此时任务已经结束或者下架删除了，拒绝后不会是3，只有进行中的拒绝才是3
                ->find();
            if($have){
                return json(['status'=>100,'msg'=>'此任务还有未审核的或者未提交的，不能删除！']);
            }else{
                $res = Db::name('task')->where('task_id',$task_id)->setField ('task_status',5);
                if($res){
                    return json(['status'=>200,'msg'=>'删除成功']);
                }else{
                    return json(['status'=>100,'msg'=>'删除失败']);
                }
            }
        }

    }


    /**
     * 获得省市区对应数据
     * @return \think\response\Json
     */
    public function getAreaByType(){
        $type = $_POST['type'];
        $id = $_POST['id'];
        $shi = Db::table('up_area')
            ->where('level',$type)
            ->where('pid',$id)
            ->field('district_id,district')
            ->select();
        return json(['status'=>200,'msg'=>$shi]);

    }
}